package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorsMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorsMapper dishFlavorsMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 新增菜品包含口味
     * @param dishDTO
     */
    @Override
    @Transactional
    public void insert(DishDTO dishDTO) {
        Dish dish=new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        //插入菜品数据
        dishMapper.insertDish(dish);
        //获取插入菜品的主键
        Long dishId = dish.getId();
        //口味表插入数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors!=null && flavors.size()>0){
            //遍历集合,把dishId存储进去
//            flavors.forEach(dishFlavor -> {
//                dishFlavor.setDishId(dishId);
//            });
            for (DishFlavor dishFlavor:flavors){
                dishFlavor.setDishId(dishId);
            }
            dishFlavorsMapper.insertFlavors(flavors);
        }

    }

    /**
     * 分页条件查询
     * @param dto
     * @return
     */
    @Override
    public PageResult pageAndOptionSelect(DishPageQueryDTO dto) {
        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        Page<DishVO> page=dishMapper.pageSelect(dto);
        log.info("输出结果:{},{}",page.getTotal(),page.getResult());

        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 删除菜品及其相关口味
     * @param ids
     */
    @Transactional
    @Override
    public void delete(List<Long> ids) {
        //菜品是否启售中

        //菜品是否关联dish_setmeal
        List<Long> setmealDishIdsByDishIds = setmealDishMapper.getSetmealDishIdsByDishIds(ids);
        if (setmealDishIdsByDishIds!=null && setmealDishIdsByDishIds.size()!=0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        //删除菜品
        dishMapper.delete(ids);
        //删除关联的dish_flavor内容
        dishFlavorsMapper.delete(ids);
    }

    /**
     * 根据id查询菜品及其口味(修改)
     * @param id
     * @return
     */
    @Override
    public DishVO selectById(Long id) {
        DishVO dishVO=new DishVO();
        Dish dish=dishMapper.selectById(id);
        List<DishFlavor> dishFlavorList=dishFlavorsMapper.selectByDishId(id);
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishFlavorList);
        return dishVO;
    }

    /**
     * 修改菜品信息
     * @param dishDTO
     */
    @Transactional
    @Override
    public void update(DishDTO dishDTO) {
        //更新菜品数据
        Dish dish=new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.updateDish(dish);
        //更新口味数据(先删除再插入)
        dishFlavorsMapper.deleteById(dish.getId());
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors!=null && flavors.size()>0){
            //遍历集合,把dishId存储进去
            for (DishFlavor dishFlavor:flavors){
                dishFlavor.setDishId(dish.getId());
            }
            dishFlavorsMapper.insertFlavors(flavors);
        }

    }

    /**
     * 菜品状态管理
     * @param status
     * @param id
     */
    @Transactional
    @Override
    public void dishStatusManage(Integer status, Long id) {
        Dish dish=new Dish();
        dish.setStatus(status);
        dish.setId(id);
        dishMapper.updateDish(dish);
        //如果菜品停售,需要将包含菜品的套餐也停售
            if (status== StatusConstant.DISABLE){
                //查询参数类型需要list集合
                List<Long>list=new ArrayList<>();
                list.add(id);
                //根据dishId获取setmealId
                List<Long> setmealIds = setmealDishMapper.getSetmealDishIdsByDishIds(list);
                //遍历集合,修改setmeal的status
                if (setmealIds!=null&&setmealIds.size()>0){
                    for (Long setmealId:setmealIds){
                        Setmeal setmeal=new Setmeal();
                        setmeal.setStatus(status);
                        setmeal.setId(setmealId);
                        setmealMapper.update(setmeal);
                    }
                }
            }
    }

    /**
     * 新增套餐-根据categoryId查询菜品
     * @param categoryId
     * @return
     */
    @Override
    public List<Dish> dishList(Long categoryId) {
        Dish dish=new Dish();
        dish.setCategoryId(categoryId);
        return dishMapper.dishlist(dish);
    }

    /**
     * 根据categoryid查询菜品及口味属性
     * @param dish
     * @return
     */
    @Override
    public List<DishVO> listWithFlavors(Dish dish) {
        List<Dish> dishlist = dishMapper.dishlist(dish);
        List<DishVO> dishVOList=new ArrayList<>();
        for (Dish dish1:dishlist){
            DishVO dishVO=new DishVO();
            BeanUtils.copyProperties(dish1,dishVO);
            //根据菜品id查询对应口味
           List<DishFlavor> flavors= dishFlavorsMapper.selectByDishId(dish1.getId());
           dishVO.setFlavors(flavors);
           dishVOList.add(dishVO);
        }

        return dishVOList;
    }

}
